home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / Snippets / Toolbox / DragWindowGrid / Application.c next >
Encoding:
C/C++ Source or Header  |  1994-12-09  |  3.3 KB  |  185 lines  |  [TEXT/MPS ]

  1. #include <QuickDraw.h>
  2. #include <Dialogs.h>
  3. #include <Fonts.h>
  4. #include <Processes.h>
  5. #include <TextEdit.h>
  6. #include <Events.h>
  7. #include <Menus.h>
  8. #include <Memory.h>
  9. #include <Errors.h>
  10. #include <ToolUtils.h>
  11.  
  12.  
  13. #ifdef __powerc
  14. QDGlobals qd;
  15. #endif
  16.  
  17.  
  18.  
  19. void InitApplication(void);
  20. void CreateNewWindow(void);
  21. void MainEventLoop(void);
  22. void MenuCommand(long whaHappened);
  23. void DoAboutBox(void);
  24.  
  25. void PreEventLoop(void);
  26. void PostEventLoop(void);
  27. pascal void DrawWindowContent(short, short, GDHandle, long);
  28. void DrawIt(WindowPtr win);
  29. void DoUpdate(WindowPtr thisWindow);
  30.  
  31.  
  32. void DragWindowGrid(WindowPtr win, Point pt);
  33.  
  34.  
  35. static Boolean gDone;
  36.  
  37.  
  38. /*-------------------------------------------------------------------------------------*/
  39.  
  40. void main()
  41. {
  42.  
  43.     InitApplication();
  44.     PreEventLoop();
  45.     MainEventLoop();
  46. }
  47.  
  48.  
  49. /*-------------------------------------------------------------------------------------*/
  50.  
  51. void InitApplication()
  52. {
  53.     Handle theMenu;
  54.  
  55.     // Toolbox initialization
  56.     MaxApplZone();
  57.     InitGraf(&qd.thePort);
  58.     InitFonts();
  59.     InitWindows();
  60.     InitMenus();
  61.     TEInit();
  62.     InitDialogs(nil);
  63.     InitCursor();
  64.     FlushEvents(0,everyEvent);
  65.     
  66.     // Application initialization
  67.     gDone = false;
  68.     
  69.     theMenu = GetNewMBar(128);
  70.     if ( theMenu == nil )
  71.         goto MenuStuffFailed;
  72.  
  73.     SetMenuBar(theMenu);
  74.     AddResMenu(GetMHandle(128), 'DRVR');
  75.     DrawMenuBar();
  76.  
  77.     return;
  78.     
  79. MenuStuffFailed:
  80.     // If the menu stuff failed, something just ain't right (most likely some 
  81.     // resources are missing.
  82.     gDone = true;
  83.     return;
  84.  
  85. }
  86.  
  87.  
  88. /*-------------------------------------------------------------------------------------*/
  89.  
  90. void DoAboutBox()
  91. {
  92.     (void) Alert(128, nil);
  93. }
  94.  
  95.  
  96. /*-------------------------------------------------------------------------------------*/
  97.  
  98. void MainEventLoop()
  99. {
  100.     EventRecord        theEvent;
  101.     WindowPtr        thisWindow;
  102.     short            clickArea;
  103.     long            menuResult;
  104.     char            charCode;
  105.  
  106.     while ( !gDone )
  107.     {
  108.         if ( WaitNextEvent(everyEvent, &theEvent, 0, nil) )
  109.         {
  110.             switch (theEvent.what)
  111.             {
  112.                 case mouseDown:
  113.                     clickArea = FindWindow(theEvent.where, &thisWindow);
  114.                     
  115.                     if (clickArea == inDrag)
  116.                     {
  117.                         DragWindowGrid(thisWindow, theEvent.where);
  118.                     }
  119.                     else if ( clickArea == inContent )
  120.                     {
  121.                         if ( thisWindow != FrontWindow() )
  122.                             SelectWindow(thisWindow);
  123.                     }
  124.                     else if (clickArea == inGoAway)
  125.                     {
  126.                         if ( TrackGoAway(thisWindow, theEvent.where) )
  127.                             gDone = true;
  128.                     }
  129.                     else if ( clickArea == inMenuBar ) 
  130.                     {
  131.                         menuResult = MenuSelect(theEvent.where);
  132.                         if ( (menuResult  >> 16) != 0 )
  133.                         {
  134.                             MenuCommand(menuResult);
  135.                             HiliteMenu(0);
  136.                         }
  137.                     }
  138.                     break;
  139.                 case keyDown:
  140.                     charCode = theEvent.message & charCodeMask;
  141.  
  142.                     if ( (theEvent.modifiers & cmdKey) != 0 ) 
  143.                     {    
  144.                         menuResult = MenuKey(charCode);
  145.                 
  146.                         if ( (menuResult  >> 16) != 0 )
  147.                             MenuCommand(menuResult);
  148.                             
  149.                     } 
  150.                     break;
  151.                 case updateEvt:
  152.                     thisWindow = (WindowPtr)theEvent.message;    
  153.                     DoUpdate(thisWindow);
  154.                 
  155.                     break;
  156.                 
  157.             }
  158.         }
  159.     }
  160. }
  161.  
  162.  
  163.  
  164. /*-------------------------------------------------------------------------------------*/
  165.  
  166. void MenuCommand(long whaHappened)
  167. {
  168.     short    menuID, menuItem;
  169.     
  170.     menuID = (whaHappened >> 16);
  171.     menuItem = (whaHappened & 0xFFFF);
  172.     
  173.     if ( menuID == 128 )
  174.     {
  175.         if ( menuItem == 1)
  176.             DoAboutBox();
  177.     }
  178.     else if ( menuID == 129 )
  179.     {
  180.         if (menuItem == 1)
  181.             gDone = true;
  182.     }
  183. }
  184.  
  185.